//
//  CategoryView.swift
//  GhostWorld
//
//  Created by Kam on 2018/4/2.
//  Copyright © 2018 LKGood. All rights reserved.
//

import UIKit

protocol CategoryViewDelegate {
  func didSelect(index: Int)
}

class CategoryView: UIView {

  // MARK: - IBOutlets
  
  @IBOutlet weak var c1View: UIView!
  @IBOutlet weak var c2View: UIView!
  @IBOutlet weak var c3View: UIView!
  @IBOutlet weak var button1: UIButton!
  @IBOutlet weak var button2: UIButton!
  @IBOutlet weak var button3: UIButton!
  @IBOutlet weak var focusView: UIView!
  
  // MARK: - Private vars
  
  private var buttons: [UIButton]?
  private var currentFocusIndex = -1
  private var isFirstLoaded = true
  
  // MARK: - Private methods 
  private func focus(_ focusButton: UIButton) {
    func set(_ button: UIButton) {
      button.isSelected = (focusButton == button ? true : false)
    }
    set(button1)
    set(button2)
    set(button3)
    
    UIView.animate(withDuration: 0.3) {
      self.focusView.frame = CGRect(
        x: focusButton.superview!.x,
        y: focusButton.superview!.maxY - 2,
        width: focusButton.superview!.width,
        height: 2
      )
    }
    delegate?.didSelect(index: focusButton.tag)
    currentFocusIndex = focusButton.tag
    
//    let spring = CASpringAnimation(keyPath: "transform.scale")
//    spring.damping = 10
//    spring.initialVelocity = 15
//    spring.mass = 0.7
//    spring.fromValue = 0.2
//    spring.toValue = 1.0
//    spring.duration = spring.settlingDuration
//    spring.isRemovedOnCompletion = true
//    focusButton.layer.add(spring, forKey: nil)
    
//    let opacity = CABasicAnimation(keyPath: "opacity")
//    opacity.fromValue = 0.0
//    opacity.toValue = 1.0
//    opacity.duration = 0.3
//    opacity.isRemovedOnCompletion = true
//    focusButton.layer.add(opacity, forKey: nil)
//    focusButton.layer.opacity = 1.0
  }
  
  // MARK: - Public vars
  
  var delegate: CategoryViewDelegate?
  
  // MARK: - Lifecycle
  
  override func layoutSubviews() {
    super.layoutSubviews()
    if isFirstLoaded {
      focus(button1)
      isFirstLoaded = false
    }
  }
  
  // MARK: - IBActions
  
  @IBAction func buttonTapped(_ sender: UIButton) {
    select(sender.tag)
  }
  
  // MARK: - Public methods
  
  func select(_ index: Int) {
    var tag = (index < 0 ? 0 : index)
    tag = (index > 2 ? 2 : index)
    if tag != currentFocusIndex {
      switch tag {
      case 1:
        focus(button2)
      case 2:
        focus(button3)
      default:
        focus(button1)
      }
    }
  }
  
}

extension CategoryView {
  class func fromNib(titles: [String], normalTitleColor: UIColor, highlightTitleColor: UIColor, focusColor: UIColor, delegate: CategoryViewDelegate? = nil) -> CategoryView? {
    
    if titles.count == 3 {
      let categoryView: CategoryView = .fromNib()
      categoryView.backgroundColor = .clear
      
      func set(_ button: UIButton, title: String) {
        button.setTitle(title, for: .normal)
        button.setTitleColor(normalTitleColor, for: .normal)
        button.setTitleColor(highlightTitleColor, for: .highlighted)
        button.setTitleColor(highlightTitleColor, for: .selected)
      }
      set(categoryView.button1, title: titles[0])
      set(categoryView.button2, title: titles[1])
      set(categoryView.button3, title: titles[2])
      
      categoryView.focusView.backgroundColor = focusColor
      categoryView.select(0)
      categoryView.delegate = delegate
      return categoryView
    } else {
      return nil
    }
  }
}
